/*
 * @Date: 2022-05-13 13:37:20
 * @LastEditors: sailornpg
 * @LastEditTime: 2022-05-18 10:19:15
 * @FilePath: \react-lvyou\src\store\index.ts
 * @Description: redux
 */
import { configureStore, getDefaultMiddleware } from "@reduxjs/toolkit";
import reducer from "./reducer";
import { changeLanguageMiddleWare } from "./middleware";
import { persistStore, persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";
// import storageSession from 'redux-persist/lib/storage/session';

// const composeEnhancers =
//   (window["__REDUX_DEVTOOLS_EXTENSION_COMPOSE__"] as typeof compose) || compose;
// const store = createStore(
//   reducer,
//   composeEnhancers(applyMiddleware(thunk, changeLanguageMiddleWare)),
// );
const storageConfig = {
  key: "root", // 必须有的
  storage, // 缓存机制
  whiteList: ["User"],
};
// 基于 localStorage 的 reducer
const persistedReducer = persistReducer(storageConfig, reducer);
const store = configureStore({
  reducer: persistedReducer,
  middleware: (getDefaultMiddleware) => [
    ...getDefaultMiddleware(),
    changeLanguageMiddleWare,
  ],
  devTools: true,
});
// 基于 localStorage 的 store、
const persistor = persistStore(store);
export type rootStateType = ReturnType<typeof store.getState>;
export default { store, persistor };
